<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!-- hexo-inject:begin --><!-- hexo-inject:end --><!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="Dragonflyxyz" />



<meta name="description" content="DES算法：
DES算法把64位的明文输入块变为64位的密文输出块，它所使用的密钥也是64位，其功能是把输入的64位数据块按位重新组合，并把输出分为L0、R0两部分，每部分各长32位，其置换规则见下表：">
<meta property="og:type" content="article">
<meta property="og:title" content="DES、AES、SHA加密算法详解">
<meta property="og:url" content="https://dragonflyxyz.github.io/2015/10/16/algorithm-detail-DES-AES-SHA/index.html">
<meta property="og:site_name" content="Dragonflyxyz的知识整理">
<meta property="og:description" content="DES算法：
DES算法把64位的明文输入块变为64位的密文输出块，它所使用的密钥也是64位，其功能是把输入的64位数据块按位重新组合，并把输出分为L0、R0两部分，每部分各长32位，其置换规则见下表：">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103100435373.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103100462960.jpg">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103101591769.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103102010146.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103102014940.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103102021588.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103102042294.png">
<meta property="og:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103102044067.png">
<meta property="og:updated_time" content="2016-10-16T11:17:03.753Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="DES、AES、SHA加密算法详解">
<meta name="twitter:description" content="DES算法：
DES算法把64位的明文输入块变为64位的密文输出块，它所使用的密钥也是64位，其功能是把输入的64位数据块按位重新组合，并把输出分为L0、R0两部分，每部分各长32位，其置换规则见下表：">
<meta name="twitter:image" content="http://pic002.cnblogs.com/images/2010/169108/2010103100435373.png">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="Dragonflyxyz的知识整理" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>DES、AES、SHA加密算法详解 | Dragonflyxyz的知识整理</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script> yiliaConfig.jquery_ui = [false]; </script>



    <script> yiliaConfig.rootUrl = "\/";</script><!-- hexo-inject:begin --><!-- hexo-inject:end -->






</head>
<body>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">Dragonflyxyz</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="false" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" href="/atom.xml" title="RSS"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/AES/">AES</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Bigdata/">Bigdata</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C#</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/DeepLearning/">DeepLearning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hadoop/">Hadoop</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/IOT/">IOT</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MIT/">MIT</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MachineLearning/">MachineLearning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Mathematics/">Mathematics</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Matlab/">Matlab</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Microsoft/">Microsoft</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Node-js/">Node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/OpenCV/">OpenCV</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/">Python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/R/">R</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RDMS/">RDMS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/RTOS/">RTOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Ruby/">Ruby</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SketchUp/">SketchUp</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vision/">Vision</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/arduino/">arduino</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/bigdata/">bigdata</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/blog/">blog</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/chip/">chip</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/code/">code</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/homedesign/">homedesign</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/intel/">intel</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mysql/">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/opensource/">opensource</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/optimize/">optimize</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/others/">others</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/picture/">picture</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/robot/">robot</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/science/">science</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/solution/">solution</a></li></ul>
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">专注于前端</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">Dragonflyxyz</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">Dragonflyxyz</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/atom.xml" title="RSS"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-algorithm-detail-DES-AES-SHA" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2015/10/16/algorithm-detail-DES-AES-SHA/" class="article-date">
      <time datetime="2015-10-16T10:54:23.000Z" itemprop="datePublished">2015-10-16</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      DES、AES、SHA加密算法详解
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/categories/algorithm/">algorithm</a>
    </div>


        
    <div class="article-tag tagcloud">
        <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/AES/">AES</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/C/">C++</a></li></ul>
    </div>

        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p>DES算法：</p>
<p>DES算法把64位的明文输入块变为64位的密文输出块，它所使用的密钥也是64位，其功能是把输入的64位数据块按位重新组合，并把输出分为L0、R0两部分，每部分各长32位，其置换规则见下表：<br><a id="more"></a><br>58,50,12,34,26,18,10,2,60,52,44,36,28,20,12,4,<br>　　62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,<br>　　57,49,41,33,25,17, 9,1,59,51,43,35,27,19,11,3,<br>　　61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,<br>　　即将输入的第58位换到第一位，第50位换到第2位，…，依此类推，最后一位是原来的第7位。L0、R0则是换位输出后的两部分，L0是输出的左 32位，R0 是右32位，例：设置换前的输入值为D1D2D3……D64，则经过初始置换后的结果为：L0=D58D50…D8；R0= D57D49…D7。<br>　　经过16次迭代运算后。得到L16、R16，将此作为输入，进行逆置换，即得到密文输出。逆置换正好是初始置的逆运算，例如，第1位经过初始置换后，处于第40位，而通过逆置换，又将第40位换回到第1位，其逆置换规则如下表所示：<br>　　40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,<br>　　38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,<br>　　36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,<br>　　34,2,42,10,50,18,58 26,33,1,41, 9,49,17,57,25,<br>放大换位表<br>　　32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10,11,<br>　　12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,<br>　　22,23,24,25,24,25,26,27,28,29,28,29,30,31,32, 1,<br>单纯换位表<br>　　16,7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,<br>　　2,8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25,<br>　　在f(Ri,Ki)算法描述图中，S1,S2…S8为选择函数，其功能是把6bit数据变为4bit数据。下面给出选择函数Si(i=1,2……8)的功能表：<br>选择函数Si<br>S1:<br>　　14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,<br>　　0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,<br>　　4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,<br>　　15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,<br>S2:<br>　　15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,<br>　　3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,<br>　　0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,<br>　　13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,<br>S3:<br>　　10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,<br>　　13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,<br>　　13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,<br>　　1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,<br>S4:<br>　　7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,<br>　　13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,<br>　　10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,<br>　　3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,<br>S5:<br>　　2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,<br>　　14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,<br>　　4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,<br>　　11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,<br>S6:<br>　　12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,<br>　　10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,<br>　　9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,<br>　　4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,<br>S7:<br>　　4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,<br>　　13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,<br>　　1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,<br>　　6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,<br>S8:<br>　　13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,<br>　　1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,<br>　　7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,<br>　　2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11,<br>在此以S1为例说明其功能，我们可以看到：在S1中，共有4行数据，命名为0，1、2、3行；每行有16列，命名为0、1、2、3，……，14、15列。<br>　　现设输入为： D＝D1D2D3D4D5D6<br>令：列＝D2D3D4D5<br>　　行＝D1D6<br>　　然后在S1表中查得对应的数，以4位二进制表示，此即为选择函数S1的输出。下面给出子密钥Ki(48bit)的生成算法<br>　　从子密钥Ki的生成算法描述图中我们可以看到：初始Key值为64位，但DES算法规定，其中第8、16、……64位是奇偶校验位，不参与 DES运算。故Key 实际可用位数便只有56位。即：经过缩小选择换位表1的变换后，Key 的位数由64 位变成了56位，此56位分为C0、D0两部分，各28位，然后分别进行第1次循环左移，得到C1、D1，将C1（28位）、D1（28位）合并得到56 位，再经过缩小选择换位2，从而便得到了密钥K0（48位）。依此类推，便可得到K1、K2、……、K15，不过需要注意的是，16次循环左移对应的左移位数要依据下述规则进行：<br>循环左移位数<br>1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1<br>　　以上介绍了DES算法的加密过程。DES算法的解密过程是一样的，区别仅仅在于第一次迭代时用子密钥K15，第二次K14、……，最后一次用K0，算法本身并没有任何变化。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div><div class="line">312</div><div class="line">313</div><div class="line">314</div><div class="line">315</div><div class="line">316</div><div class="line">317</div><div class="line">318</div><div class="line">319</div><div class="line">320</div><div class="line">321</div><div class="line">322</div><div class="line">323</div><div class="line">324</div><div class="line">325</div><div class="line">326</div><div class="line">327</div><div class="line">328</div><div class="line">329</div><div class="line">330</div><div class="line">331</div><div class="line">332</div><div class="line">333</div><div class="line">334</div><div class="line">335</div><div class="line">336</div><div class="line">337</div><div class="line">338</div><div class="line">339</div><div class="line">340</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//DES算法加密  </span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span>  </span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;math.h&gt;</span>  </span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line"><span class="function"><span class="keyword">char</span> * <span class="title">str2bin</span><span class="params">(<span class="keyword">char</span> *str)</span></span>&#123;<span class="comment">//将16位16进制码转换为64位二进制码(不足8位高位补0)  </span></div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">char</span> *st2;  </div><div class="line">    st2=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">65</span>);  </div><div class="line">    st2[<span class="number">0</span>]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">16</span>;i++)&#123;  </div><div class="line">        <span class="keyword">switch</span>(str[i])&#123;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'0'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0000"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'1'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0001"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'2'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0010"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'3'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0011"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'4'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0100"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'5'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0101"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'6'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0110"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'7'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"0111"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'8'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1000"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'9'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1001"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'A'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1010"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'B'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1011"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'C'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1100"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'D'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1101"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'E'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1110"</span>);<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="string">'F'</span>:<span class="built_in">strcat</span>(st2,<span class="string">"1111"</span>);<span class="keyword">break</span>;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    st2[<span class="number">64</span>]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> st2;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span> * <span class="title">bin2str</span><span class="params">(<span class="keyword">char</span> *st2)</span></span>&#123;<span class="comment">//将64位二进制码转换为16位16进制码  </span></div><div class="line">    <span class="keyword">int</span> i,j;  </div><div class="line">    <span class="keyword">char</span> *st;  </div><div class="line">    <span class="keyword">int</span> a[]=&#123;<span class="number">8</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">1</span>&#125;;  </div><div class="line">    <span class="keyword">int</span> buf;  </div><div class="line">    st=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">17</span>);  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">16</span>;i++)&#123;  </div><div class="line">        buf=<span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">4</span>;j++)&#123;  </div><div class="line">            buf=buf+(st2[i*<span class="number">4</span>+j]==<span class="string">'1'</span>? a[j]:<span class="number">0</span>);  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">switch</span>(buf)&#123;  </div><div class="line">        <span class="keyword">case</span> <span class="number">0</span>: st[i]=<span class="string">'0'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">1</span>: st[i]=<span class="string">'1'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">2</span>: st[i]=<span class="string">'2'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">3</span>: st[i]=<span class="string">'3'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">4</span>: st[i]=<span class="string">'4'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">5</span>: st[i]=<span class="string">'5'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">6</span>: st[i]=<span class="string">'6'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">7</span>: st[i]=<span class="string">'7'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">8</span>: st[i]=<span class="string">'8'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">9</span>: st[i]=<span class="string">'9'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">10</span>: st[i]=<span class="string">'A'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">11</span>: st[i]=<span class="string">'B'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">12</span>: st[i]=<span class="string">'C'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">13</span>: st[i]=<span class="string">'D'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">14</span>: st[i]=<span class="string">'E'</span>;<span class="keyword">break</span>;  </div><div class="line">        <span class="keyword">case</span> <span class="number">15</span>: st[i]=<span class="string">'F'</span>;<span class="keyword">break</span>;  </div><div class="line">        &#125;          </div><div class="line">    &#125;  </div><div class="line">    st[<span class="number">16</span>]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> st;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span> * <span class="title">chushizhihuan</span><span class="params">(<span class="keyword">char</span> *strb)</span></span>&#123;<span class="comment">//1.初始置换  </span></div><div class="line">    <span class="keyword">char</span> *stra;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    stra=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">65</span>);  </div><div class="line">    <span class="keyword">int</span> a[<span class="number">64</span>] = &#123;<span class="number">57</span>,<span class="number">49</span>,<span class="number">41</span>,<span class="number">33</span>,<span class="number">25</span>,<span class="number">17</span>,<span class="number">9</span>,<span class="number">1</span>,  </div><div class="line">,<span class="number">51</span>,<span class="number">43</span>,<span class="number">35</span>,<span class="number">27</span>,<span class="number">19</span>,<span class="number">11</span>,<span class="number">3</span>,  </div><div class="line">,<span class="number">53</span>,<span class="number">45</span>,<span class="number">37</span>,<span class="number">29</span>,<span class="number">21</span>,<span class="number">13</span>,<span class="number">5</span>,  </div><div class="line">,<span class="number">55</span>,<span class="number">47</span>,<span class="number">39</span>,<span class="number">31</span>,<span class="number">23</span>,<span class="number">15</span>,<span class="number">7</span>,  </div><div class="line">,<span class="number">48</span>,<span class="number">40</span>,<span class="number">32</span>,<span class="number">24</span>,<span class="number">16</span>,<span class="number">8</span>,<span class="number">0</span>,  </div><div class="line">,<span class="number">50</span>,<span class="number">42</span>,<span class="number">34</span>,<span class="number">26</span>,<span class="number">18</span>,<span class="number">10</span>,<span class="number">2</span>,  </div><div class="line">,<span class="number">52</span>,<span class="number">44</span>,<span class="number">36</span>,<span class="number">28</span>,<span class="number">20</span>,<span class="number">12</span>,<span class="number">4</span>,  </div><div class="line">,<span class="number">54</span>,<span class="number">46</span>,<span class="number">38</span>,<span class="number">30</span>,<span class="number">22</span>,<span class="number">14</span>,<span class="number">6</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">64</span>;i++)  </div><div class="line">        stra[i]=strb[a[i]];  </div><div class="line">    stra[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> stra;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span>* <span class="title">miyuezhihuan</span><span class="params">(<span class="keyword">char</span> *keyb)</span></span>&#123;<span class="comment">//3.密钥置换  </span></div><div class="line">    <span class="keyword">char</span> *keya;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    keya=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">57</span>);  </div><div class="line">  </div><div class="line">    <span class="keyword">int</span> a[]=&#123;<span class="number">56</span>,<span class="number">48</span>,<span class="number">40</span>,<span class="number">32</span>,<span class="number">24</span>,<span class="number">16</span>,<span class="number">8</span>,  </div><div class="line">,<span class="number">57</span>,<span class="number">49</span>,<span class="number">41</span>,<span class="number">33</span>,<span class="number">25</span>,<span class="number">17</span>,  </div><div class="line">,<span class="number">1</span>,<span class="number">58</span>,<span class="number">50</span>,<span class="number">42</span>,<span class="number">34</span>,<span class="number">26</span>,  </div><div class="line">,<span class="number">10</span>,<span class="number">2</span>,<span class="number">59</span>,<span class="number">51</span>,<span class="number">43</span>,<span class="number">35</span>,  </div><div class="line">,<span class="number">54</span>,<span class="number">46</span>,<span class="number">38</span>,<span class="number">30</span>,<span class="number">22</span>,<span class="number">14</span>,  </div><div class="line">,<span class="number">61</span>,<span class="number">53</span>,<span class="number">45</span>,<span class="number">37</span>,<span class="number">29</span>,<span class="number">21</span>,  </div><div class="line">,<span class="number">5</span>,<span class="number">60</span>,<span class="number">52</span>,<span class="number">44</span>,<span class="number">36</span>,<span class="number">28</span>,  </div><div class="line">,<span class="number">12</span>,<span class="number">4</span>,<span class="number">27</span>,<span class="number">19</span>,<span class="number">11</span>,<span class="number">3</span>&#125;;  </div><div class="line">  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">56</span>;i++)&#123;  </div><div class="line">        keya[i]=keyb[a[i]];  </div><div class="line">    &#125;  </div><div class="line">    keya[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> keya;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">miyuezuoyi</span><span class="params">(<span class="keyword">char</span> *keyb,<span class="keyword">int</span> n)</span></span>&#123;<span class="comment">//4.第n轮密钥左移  </span></div><div class="line">    <span class="keyword">char</span> *temp;  </div><div class="line">    <span class="keyword">int</span> a[]=&#123;<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,  </div><div class="line">,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">1</span>&#125;;  </div><div class="line">    temp=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">57</span>);  </div><div class="line">  </div><div class="line">    <span class="comment">//保存将要循环移动到右边的位    </span></div><div class="line">    <span class="built_in">memcpy</span>(temp,keyb,a[n]);    </div><div class="line">    <span class="built_in">memcpy</span>(temp+a[n],keyb+<span class="number">28</span>,a[n]);    </div><div class="line">        </div><div class="line">    <span class="comment">//前28位移动    </span></div><div class="line">    <span class="built_in">memcpy</span>(keyb,keyb+a[n],<span class="number">28</span>-a[n]);    </div><div class="line">    <span class="built_in">memcpy</span>(keyb+<span class="number">28</span>-a[n],temp,a[n]);    </div><div class="line">    </div><div class="line">    <span class="comment">//后28位移动    </span></div><div class="line">    <span class="built_in">memcpy</span>(keyb+<span class="number">28</span>,keyb+<span class="number">28</span>+a[n],<span class="number">28</span>-a[n]);    </div><div class="line">    <span class="built_in">memcpy</span>(keyb+<span class="number">56</span>-a[n],temp+a[n],a[n]);   </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">char</span> * <span class="title">miyueyasuozhihuan</span><span class="params">(<span class="keyword">char</span> *key)</span></span>&#123;<span class="comment">//5.密钥压缩置换(产生子密钥)  </span></div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">char</span> *keybuf;  </div><div class="line">    keybuf=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">49</span>);  </div><div class="line">    <span class="keyword">int</span> a[]=&#123;<span class="number">13</span>,<span class="number">16</span>,<span class="number">10</span>,<span class="number">23</span>,<span class="number">0</span>,<span class="number">4</span>,  </div><div class="line">,<span class="number">27</span>,<span class="number">14</span>,<span class="number">5</span>,<span class="number">20</span>,<span class="number">9</span>,  </div><div class="line">,<span class="number">18</span>,<span class="number">11</span>,<span class="number">3</span>,<span class="number">25</span>,<span class="number">7</span>,  </div><div class="line">,<span class="number">6</span>,<span class="number">26</span>,<span class="number">19</span>,<span class="number">12</span>,<span class="number">1</span>,  </div><div class="line">,<span class="number">51</span>,<span class="number">30</span>,<span class="number">36</span>,<span class="number">46</span>,<span class="number">54</span>,  </div><div class="line">,<span class="number">39</span>,<span class="number">50</span>,<span class="number">44</span>,<span class="number">32</span>,<span class="number">47</span>,  </div><div class="line">,<span class="number">48</span>,<span class="number">38</span>,<span class="number">55</span>,<span class="number">33</span>,<span class="number">52</span>,  </div><div class="line">,<span class="number">41</span>,<span class="number">49</span>,<span class="number">35</span>,<span class="number">28</span>,<span class="number">31</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">48</span>;i++)&#123;  </div><div class="line">        keybuf[i]=key[a[i]];  </div><div class="line">    &#125;  </div><div class="line">    keybuf[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> keybuf;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span> * <span class="title">kuozhanzhihuan</span><span class="params">(<span class="keyword">char</span> *st)</span></span>&#123;<span class="comment">//6.扩展置换  </span></div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">char</span> *stbuf;  </div><div class="line">    stbuf=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">49</span>);  </div><div class="line">    <span class="keyword">int</span> a[<span class="number">48</span>] = &#123;<span class="number">31</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>,  </div><div class="line">,  <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>,  </div><div class="line">,  <span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>,  </div><div class="line">,<span class="number">12</span>,<span class="number">13</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">16</span>,  </div><div class="line">,<span class="number">16</span>,<span class="number">17</span>,<span class="number">18</span>,<span class="number">19</span>,<span class="number">20</span>,  </div><div class="line">,<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">23</span>,<span class="number">24</span>,  </div><div class="line">,<span class="number">24</span>,<span class="number">25</span>,<span class="number">26</span>,<span class="number">27</span>,<span class="number">28</span>,  </div><div class="line">,<span class="number">28</span>,<span class="number">29</span>,<span class="number">30</span>,<span class="number">31</span>, <span class="number">0</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">48</span>;i++)  </div><div class="line">        stbuf[i]=st[a[i]];  </div><div class="line">    stbuf[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> stbuf;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">yihuoyunsuan</span><span class="params">(<span class="keyword">char</span> * str,<span class="keyword">char</span> *key,<span class="keyword">int</span> n)</span></span>&#123;<span class="comment">//7.异或运算  </span></div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;n;i++)&#123;  </div><div class="line">        str[i]=(str[i]==key[i])? <span class="string">'0'</span>:<span class="string">'1'</span>;  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">bin2int</span><span class="params">(<span class="keyword">char</span> *bin)</span></span>&#123;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">int</span> Int;  </div><div class="line">    Int=<span class="number">0</span>;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="built_in">strlen</span>(bin);i++)  </div><div class="line">        <span class="keyword">if</span>(bin[i]==<span class="string">'1'</span>)  </div><div class="line">            Int=Int+<span class="built_in">pow</span>(<span class="number">2</span>,<span class="built_in">strlen</span>(bin)-i<span class="number">-1</span>);  </div><div class="line">    <span class="keyword">return</span> Int;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span> *<span class="title">int2bin</span><span class="params">(<span class="keyword">int</span> Int)</span></span>&#123;  </div><div class="line">    <span class="keyword">char</span> *bin;  </div><div class="line">    <span class="keyword">int</span> key;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    key=<span class="number">8</span>;  </div><div class="line">    bin=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">5</span>);  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">4</span>;i++)&#123;  </div><div class="line">        bin[i]=(key&amp;Int)? <span class="string">'1'</span>:<span class="string">'0'</span> ;  </div><div class="line">        key&gt;&gt;=<span class="number">1</span>;  </div><div class="line">    &#125;  </div><div class="line">    bin[<span class="number">4</span>]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> bin;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span>* <span class="title">Shedaiti</span><span class="params">(<span class="keyword">char</span> *strb)</span></span>&#123;<span class="comment">//8.S盒代替  </span></div><div class="line">    <span class="keyword">int</span> i,j;  </div><div class="line">    <span class="keyword">char</span> *stra;  </div><div class="line">    <span class="keyword">char</span> *hang;  </div><div class="line">    <span class="keyword">char</span> *lie;  </div><div class="line">    stra=(<span class="keyword">char</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">33</span>);  </div><div class="line">    hang=(<span class="keyword">char</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">3</span>);  </div><div class="line">    lie=(<span class="keyword">char</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">5</span>);  </div><div class="line">    stra[<span class="number">0</span>]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">int</span> s[<span class="number">8</span>][<span class="number">1</span>][<span class="number">16</span>]=<span class="comment">//S1  </span></div><div class="line">              &#123;&#123;&#123;<span class="number">14</span>,<span class="number">4</span>,<span class="number">13</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">15</span>,<span class="number">11</span>,<span class="number">8</span>,<span class="number">3</span>,<span class="number">10</span>,<span class="number">6</span>,<span class="number">12</span>,<span class="number">5</span>,<span class="number">9</span>,<span class="number">0</span>,<span class="number">7</span>&#125;,  </div><div class="line">                &#123;<span class="number">0</span>,<span class="number">15</span>,<span class="number">7</span>,<span class="number">4</span>,<span class="number">14</span>,<span class="number">2</span>,<span class="number">13</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">6</span>,<span class="number">12</span>,<span class="number">11</span>,<span class="number">9</span>,<span class="number">5</span>,<span class="number">3</span>,<span class="number">8</span>&#125;,  </div><div class="line">                &#123;<span class="number">4</span>,<span class="number">1</span>,<span class="number">14</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">6</span>,<span class="number">2</span>,<span class="number">11</span>,<span class="number">15</span>,<span class="number">12</span>,<span class="number">9</span>,<span class="number">7</span>,<span class="number">3</span>,<span class="number">10</span>,<span class="number">5</span>,<span class="number">0</span>&#125;,  </div><div class="line">                &#123;<span class="number">15</span>,<span class="number">12</span>,<span class="number">8</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">1</span>,<span class="number">7</span>,<span class="number">5</span>,<span class="number">11</span>,<span class="number">3</span>,<span class="number">14</span>,<span class="number">10</span>,<span class="number">0</span>,<span class="number">6</span>,<span class="number">13</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S2  </span></div><div class="line">                &#123;&#123;<span class="number">15</span>,<span class="number">1</span>,<span class="number">8</span>,<span class="number">14</span>,<span class="number">6</span>,<span class="number">11</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">7</span>,<span class="number">2</span>,<span class="number">13</span>,<span class="number">12</span>,<span class="number">0</span>,<span class="number">5</span>,<span class="number">10</span>&#125;,  </div><div class="line">                &#123;<span class="number">3</span>,<span class="number">13</span>,<span class="number">4</span>,<span class="number">7</span>,<span class="number">15</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">14</span>,<span class="number">12</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">11</span>,<span class="number">5</span>&#125;,  </div><div class="line">                &#123;<span class="number">0</span>,<span class="number">14</span>,<span class="number">7</span>,<span class="number">11</span>,<span class="number">10</span>,<span class="number">4</span>,<span class="number">13</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">12</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">15</span>&#125;,  </div><div class="line">                &#123;<span class="number">13</span>,<span class="number">8</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">15</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">11</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">12</span>,<span class="number">0</span>,<span class="number">5</span>,<span class="number">14</span>,<span class="number">9</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S3  </span></div><div class="line">                &#123;&#123;<span class="number">10</span>,<span class="number">0</span>,<span class="number">9</span>,<span class="number">14</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">15</span>,<span class="number">5</span>,<span class="number">1</span>,<span class="number">13</span>,<span class="number">12</span>,<span class="number">7</span>,<span class="number">11</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">8</span>&#125;,  </div><div class="line">                &#123;<span class="number">13</span>,<span class="number">7</span>,<span class="number">0</span>,<span class="number">9</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">10</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">5</span>,<span class="number">14</span>,<span class="number">12</span>,<span class="number">11</span>,<span class="number">15</span>,<span class="number">1</span>&#125;,  </div><div class="line">                  &#123;<span class="number">13</span>,<span class="number">6</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">8</span>,<span class="number">15</span>,<span class="number">3</span>,<span class="number">0</span>,<span class="number">11</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">5</span>,<span class="number">10</span>,<span class="number">14</span>,<span class="number">7</span>&#125;,  </div><div class="line">                &#123;<span class="number">1</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">0</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">8</span>,<span class="number">7</span>,<span class="number">4</span>,<span class="number">15</span>,<span class="number">14</span>,<span class="number">3</span>,<span class="number">11</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">12</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S4  </span></div><div class="line">                &#123;&#123;<span class="number">7</span>,<span class="number">13</span>,<span class="number">14</span>,<span class="number">3</span>,<span class="number">0</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">5</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">4</span>,<span class="number">15</span>&#125;,  </div><div class="line">                &#123;<span class="number">13</span>,<span class="number">8</span>,<span class="number">11</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">15</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">7</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">14</span>,<span class="number">9</span>&#125;,  </div><div class="line">                &#123;<span class="number">10</span>,<span class="number">6</span>,<span class="number">9</span>,<span class="number">0</span>,<span class="number">12</span>,<span class="number">11</span>,<span class="number">7</span>,<span class="number">13</span>,<span class="number">15</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">14</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">4</span>&#125;,  </div><div class="line">                &#123;<span class="number">3</span>,<span class="number">15</span>,<span class="number">0</span>,<span class="number">6</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="number">13</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">11</span>,<span class="number">12</span>,<span class="number">7</span>,<span class="number">2</span>,<span class="number">14</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S5  </span></div><div class="line">                &#123;&#123;<span class="number">2</span>,<span class="number">12</span>,<span class="number">4</span>,<span class="number">1</span>,<span class="number">7</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">6</span>,<span class="number">8</span>,<span class="number">5</span>,<span class="number">3</span>,<span class="number">15</span>,<span class="number">13</span>,<span class="number">0</span>,<span class="number">14</span>,<span class="number">9</span>&#125;,  </div><div class="line">                &#123;<span class="number">14</span>,<span class="number">11</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">4</span>,<span class="number">7</span>,<span class="number">13</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">0</span>,<span class="number">15</span>,<span class="number">10</span>,<span class="number">3</span>,<span class="number">9</span>,<span class="number">8</span>,<span class="number">6</span>&#125;,  </div><div class="line">                &#123;<span class="number">4</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">11</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">15</span>,<span class="number">9</span>,<span class="number">12</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">0</span>,<span class="number">14</span>&#125;,  </div><div class="line">                &#123;<span class="number">11</span>,<span class="number">8</span>,<span class="number">12</span>,<span class="number">7</span>,<span class="number">1</span>,<span class="number">14</span>,<span class="number">2</span>,<span class="number">13</span>,<span class="number">6</span>,<span class="number">15</span>,<span class="number">0</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">3</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S6  </span></div><div class="line">                &#123;&#123;<span class="number">12</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">15</span>,<span class="number">9</span>,<span class="number">2</span>,<span class="number">6</span>,<span class="number">8</span>,<span class="number">0</span>,<span class="number">13</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">14</span>,<span class="number">7</span>,<span class="number">5</span>,<span class="number">11</span>&#125;,  </div><div class="line">                &#123;<span class="number">10</span>,<span class="number">15</span>,<span class="number">4</span>,<span class="number">2</span>,<span class="number">7</span>,<span class="number">12</span>,<span class="number">9</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">1</span>,<span class="number">13</span>,<span class="number">14</span>,<span class="number">0</span>,<span class="number">11</span>,<span class="number">3</span>,<span class="number">8</span>&#125;,  </div><div class="line">                &#123;<span class="number">9</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">12</span>,<span class="number">3</span>,<span class="number">7</span>,<span class="number">0</span>,<span class="number">4</span>,<span class="number">10</span>,<span class="number">1</span>,<span class="number">13</span>,<span class="number">11</span>,<span class="number">6</span>&#125;,  </div><div class="line">                &#123;<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">12</span>,<span class="number">9</span>,<span class="number">5</span>,<span class="number">15</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">14</span>,<span class="number">1</span>,<span class="number">7</span>,<span class="number">6</span>,<span class="number">0</span>,<span class="number">8</span>,<span class="number">13</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S7  </span></div><div class="line">                &#123;&#123;<span class="number">4</span>,<span class="number">11</span>,<span class="number">2</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">0</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">3</span>,<span class="number">12</span>,<span class="number">9</span>,<span class="number">7</span>,<span class="number">5</span>,<span class="number">10</span>,<span class="number">6</span>,<span class="number">1</span>&#125;,  </div><div class="line">                &#123;<span class="number">13</span>,<span class="number">0</span>,<span class="number">11</span>,<span class="number">7</span>,<span class="number">4</span>,<span class="number">9</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">14</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">12</span>,<span class="number">2</span>,<span class="number">15</span>,<span class="number">8</span>,<span class="number">6</span>&#125;,  </div><div class="line">                &#123;<span class="number">1</span>,<span class="number">4</span>,<span class="number">11</span>,<span class="number">13</span>,<span class="number">12</span>,<span class="number">3</span>,<span class="number">7</span>,<span class="number">14</span>,<span class="number">10</span>,<span class="number">15</span>,<span class="number">6</span>,<span class="number">8</span>,<span class="number">0</span>,<span class="number">5</span>,<span class="number">9</span>,<span class="number">2</span>&#125;,  </div><div class="line">                &#123;<span class="number">6</span>,<span class="number">11</span>,<span class="number">13</span>,<span class="number">8</span>,<span class="number">1</span>,<span class="number">4</span>,<span class="number">10</span>,<span class="number">7</span>,<span class="number">9</span>,<span class="number">5</span>,<span class="number">0</span>,<span class="number">15</span>,<span class="number">14</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">12</span>&#125;&#125;,  </div><div class="line">                <span class="comment">//S8  </span></div><div class="line">                &#123;&#123;<span class="number">13</span>,<span class="number">2</span>,<span class="number">8</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">15</span>,<span class="number">11</span>,<span class="number">1</span>,<span class="number">10</span>,<span class="number">9</span>,<span class="number">3</span>,<span class="number">14</span>,<span class="number">5</span>,<span class="number">0</span>,<span class="number">12</span>,<span class="number">7</span>&#125;,  </div><div class="line">                &#123;<span class="number">1</span>,<span class="number">15</span>,<span class="number">13</span>,<span class="number">8</span>,<span class="number">10</span>,<span class="number">3</span>,<span class="number">7</span>,<span class="number">4</span>,<span class="number">12</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">11</span>,<span class="number">0</span>,<span class="number">14</span>,<span class="number">9</span>,<span class="number">2</span>&#125;,  </div><div class="line">                &#123;<span class="number">7</span>,<span class="number">11</span>,<span class="number">4</span>,<span class="number">1</span>,<span class="number">9</span>,<span class="number">12</span>,<span class="number">14</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">6</span>,<span class="number">10</span>,<span class="number">13</span>,<span class="number">15</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">8</span>&#125;,  </div><div class="line">                &#123;<span class="number">2</span>,<span class="number">1</span>,<span class="number">14</span>,<span class="number">7</span>,<span class="number">4</span>,<span class="number">10</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">15</span>,<span class="number">12</span>,<span class="number">9</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">11</span>&#125;&#125;&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++)&#123;  </div><div class="line">        hang[<span class="number">0</span>]=strb[i*<span class="number">6</span>];  </div><div class="line">        hang[<span class="number">1</span>]=strb[i*<span class="number">6</span>+<span class="number">5</span>];  </div><div class="line">        hang[<span class="number">2</span>]=<span class="string">'\0'</span>;  </div><div class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">4</span>;j++)  </div><div class="line">            lie[j]=strb[i*<span class="number">6</span>+j+<span class="number">1</span>];  </div><div class="line">        lie[j]=<span class="string">'\0'</span>;  </div><div class="line">        <span class="built_in">strcat</span>(stra,int2bin(s[i][bin2int(hang)][bin2int(lie)]));  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> stra;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">char</span> *<span class="title">Phezhihuan</span><span class="params">(<span class="keyword">char</span> *strb)</span></span>&#123;<span class="comment">//9.P盒置换  </span></div><div class="line">    <span class="keyword">char</span> *stra;  </div><div class="line">    stra=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">33</span>);  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">int</span> P[]= &#123;<span class="number">15</span>,<span class="number">6</span>,<span class="number">19</span>,<span class="number">20</span>,<span class="number">28</span>,<span class="number">11</span>,<span class="number">27</span>,<span class="number">16</span>,  </div><div class="line">,<span class="number">14</span>,<span class="number">22</span>,<span class="number">25</span>,<span class="number">4</span>,<span class="number">17</span>,<span class="number">30</span>,<span class="number">9</span>,  </div><div class="line">,<span class="number">7</span>,<span class="number">23</span>,<span class="number">13</span>,<span class="number">31</span>,<span class="number">26</span>,<span class="number">2</span>,<span class="number">8</span>,  </div><div class="line">,<span class="number">12</span>,<span class="number">29</span>,<span class="number">5</span>,<span class="number">21</span>,<span class="number">10</span>,<span class="number">3</span>,<span class="number">24</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">32</span>;i++)  </div><div class="line">        stra[i]=strb[P[i]];  </div><div class="line">    stra[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> stra;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span> *<span class="title">hebing</span><span class="params">(<span class="keyword">char</span> *strl,<span class="keyword">char</span> *strr)</span></span>&#123;<span class="comment">//13.合并  </span></div><div class="line">    <span class="keyword">char</span> *str;  </div><div class="line">    str=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">65</span>);  </div><div class="line">    <span class="built_in">strcpy</span>(str,strl);  </div><div class="line">    <span class="built_in">strcat</span>(str,strr);  </div><div class="line">    <span class="keyword">return</span> str;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">char</span>* <span class="title">nichushizhihuan</span><span class="params">(<span class="keyword">char</span> *strb)</span></span>&#123;<span class="comment">//13.逆初始置换  </span></div><div class="line">    <span class="keyword">char</span> *stra;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    stra=(<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">65</span>);  </div><div class="line">    <span class="keyword">int</span> a[<span class="number">64</span>]= &#123;<span class="number">39</span>,<span class="number">7</span>,<span class="number">47</span>,<span class="number">15</span>,<span class="number">55</span>,<span class="number">23</span>,<span class="number">63</span>,<span class="number">31</span>,  </div><div class="line">,<span class="number">6</span>,<span class="number">46</span>,<span class="number">14</span>,<span class="number">54</span>,<span class="number">22</span>,<span class="number">62</span>,<span class="number">30</span>,  </div><div class="line">,<span class="number">5</span>,<span class="number">45</span>,<span class="number">13</span>,<span class="number">53</span>,<span class="number">21</span>,<span class="number">61</span>,<span class="number">29</span>,  </div><div class="line">,<span class="number">4</span>,<span class="number">44</span>,<span class="number">12</span>,<span class="number">52</span>,<span class="number">20</span>,<span class="number">60</span>,<span class="number">28</span>,  </div><div class="line">,<span class="number">3</span>,<span class="number">43</span>,<span class="number">11</span>,<span class="number">51</span>,<span class="number">19</span>,<span class="number">59</span>,<span class="number">27</span>,  </div><div class="line">,<span class="number">2</span>,<span class="number">42</span>,<span class="number">10</span>,<span class="number">50</span>,<span class="number">18</span>,<span class="number">58</span>,<span class="number">26</span>,  </div><div class="line">,<span class="number">1</span>,<span class="number">41</span>,<span class="number">9</span>,<span class="number">49</span>,<span class="number">17</span>,<span class="number">57</span>,<span class="number">25</span>,  </div><div class="line">,<span class="number">0</span>,<span class="number">40</span>,<span class="number">8</span>,<span class="number">48</span>,<span class="number">16</span>,<span class="number">56</span>,<span class="number">24</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">64</span>;i++)&#123;  </div><div class="line">        stra[i]=strb[a[i]];  </div><div class="line">    &#125;  </div><div class="line">    stra[i]=<span class="string">'\0'</span>;  </div><div class="line">    <span class="keyword">return</span> stra;  </div><div class="line">&#125;  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">swap</span><span class="params">(<span class="keyword">char</span> *left, <span class="keyword">char</span> *right)</span></span>&#123;    </div><div class="line">    <span class="keyword">char</span> *temp;  </div><div class="line">    temp=(<span class="keyword">char</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">33</span>);  </div><div class="line">    <span class="built_in">memcpy</span>(temp,left,<span class="number">32</span>);       </div><div class="line">    <span class="built_in">memcpy</span>(left,right,<span class="number">32</span>);      </div><div class="line">    <span class="built_in">memcpy</span>(right,temp,<span class="number">32</span>);    </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;    </div><div class="line">&#125;   </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;  </div><div class="line">    <span class="keyword">int</span> j;  </div><div class="line">    <span class="keyword">char</span> *str_Ox;  </div><div class="line">    <span class="keyword">char</span> *str_64;  </div><div class="line">    <span class="keyword">char</span> *str_R_48;  </div><div class="line">    <span class="keyword">char</span> *key_Ox;  </div><div class="line">    <span class="keyword">char</span> *key_56;  </div><div class="line">    <span class="keyword">char</span> *key_48;  </div><div class="line">  </div><div class="line">    str_Ox = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">17</span>);  </div><div class="line">    str_64 = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">65</span>);  </div><div class="line">    str_R_48 = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">49</span>);  </div><div class="line">    key_Ox  = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">17</span>);  </div><div class="line">    key_56  = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">57</span>);  </div><div class="line">    key_48  = (<span class="keyword">char</span>*)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="keyword">char</span>)*<span class="number">49</span>);<span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%s %s"</span>,key_Ox,str_Ox)!=EOF)&#123;<span class="comment">//获取16位16进制密钥和16位16进制原文  </span></div><div class="line">  </div><div class="line">        key_56 = miyuezhihuan(str2bin(key_Ox));<span class="comment">//将字符密钥转换为01密钥并执行密钥置换  </span></div><div class="line">  </div><div class="line">        str_64 = chushizhihuan(str2bin(str_Ox));<span class="comment">//将字符原文转换为01原文并执行初始置换  </span></div><div class="line">  </div><div class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">16</span>;j++)&#123;      </div><div class="line">  </div><div class="line">            <span class="built_in">memcpy</span>(str_R_48,str_64+<span class="number">32</span>,<span class="number">33</span>);<span class="comment">//分组  </span></div><div class="line">      </div><div class="line">            miyuezuoyi(key_56,j);<span class="comment">//4.密钥左移  </span></div><div class="line">  </div><div class="line">            key_48 = miyueyasuozhihuan(key_56);<span class="comment">//5.密钥压缩置换  </span></div><div class="line">  </div><div class="line">            str_R_48 = kuozhanzhihuan(str_R_48);<span class="comment">//6.扩展置换  </span></div><div class="line">  </div><div class="line">            yihuoyunsuan(str_R_48,key_48,<span class="number">48</span>);<span class="comment">//7.48位异或运算  </span></div><div class="line">  </div><div class="line">            str_R_48 = Shedaiti(str_R_48);<span class="comment">//8.S盒代替  </span></div><div class="line">  </div><div class="line">            str_R_48 = Phezhihuan(str_R_48);<span class="comment">//9.P盒置换  </span></div><div class="line">  </div><div class="line">            yihuoyunsuan(str_64,str_R_48,<span class="number">32</span>);<span class="comment">//10.32位异或运算  </span></div><div class="line">      </div><div class="line">            <span class="keyword">if</span>(j!=<span class="number">15</span>)&#123;  </div><div class="line">                swap(str_64,str_64+<span class="number">32</span>);  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">        str_64 = nichushizhihuan(str_64);  </div><div class="line">        str_Ox = bin2str(str_64);  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%s\n"</span>,str_Ox);  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2>AES算法</h2>


<p>先搞定AES算法，基本变换包括SubBytes（字节替代）、ShiftRows（行移位）、MixColumns（列混淆）、AddRoundKey(轮密钥加）</p>
<p>其算法一般描述为<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103100435373.png" alt="enter description here"><br>明文及密钥的组织排列方式<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103100462960.jpg" alt="enter description here"></p>
<h3>ByteSubstitution（字节替代）</h3>

<p>非线性的字节替代，单独处理每个字节：</p>
<p>求该字节在有限域GF(28)上的乘法逆，”0”被映射为自身，即对于α∈GF(28)，求β∈GF(28)，</p>
<p>使得α·β=β·α=1mod(x8+x4+x2+x+1)。</p>
<p>对上一步求得的乘法逆作仿射变换</p>
<p>yi=xi + x(i+4)mod8 + x(i+6)mod8 + x(i+7)mod8 + ci</p>
<p>(其中ci是6310即011000112的第i位），用矩阵表示为<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103101591769.png" alt="enter description here"><br>本来打算把求乘法逆和仿射变换算法敲上去，最后还是放弃了…直接打置换表</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">unsigned char sBox[] =  </div><div class="line">&#123; /*  0    1    2    3    4    5    6    7    8    9    a    b    c    d    e    f */  </div><div class="line">    0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, /*0*/   </div><div class="line">    0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, /*1*/  </div><div class="line">    0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, /*2*/  </div><div class="line">    0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, /*3*/  </div><div class="line">    0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, /*4*/  </div><div class="line">    0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, /*5*/  </div><div class="line">    0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, /*6*/   </div><div class="line">    0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, /*7*/  </div><div class="line">    0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, /*8*/  </div><div class="line">    0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, /*9*/  </div><div class="line">    0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, /*a*/  </div><div class="line">    0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, /*b*/  </div><div class="line">    0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, /*c*/  </div><div class="line">    0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, /*d*/  </div><div class="line">    0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, /*e*/  </div><div class="line">    0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16  /*f*/  </div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>下面是逆置换表，解密时使用</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">unsigned char invsBox[256] =   </div><div class="line">&#123; /*  0    1    2    3    4    5    6    7    8    9    a    b    c    d    e    f  */   </div><div class="line">    0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb, /*0*/  </div><div class="line">    0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb, /*1*/  </div><div class="line">    0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e, /*2*/  </div><div class="line">    0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25, /*3*/  </div><div class="line">    0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92, /*4*/  </div><div class="line">    0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84, /*5*/  </div><div class="line">    0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06, /*6*/  </div><div class="line">    0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b, /*7*/  </div><div class="line">    0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73, /*8*/  </div><div class="line">    0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e, /*9*/  </div><div class="line">    0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b, /*a*/  </div><div class="line">    0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4, /*b*/  </div><div class="line">    0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f, /*c*/  </div><div class="line">    0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef, /*d*/  </div><div class="line">    0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61, /*e*/  </div><div class="line">    0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d  /*f*/  </div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>这里遇到问题了，本来用纯c初始化数组很正常，封装成类以后发现不能初始化，不管是声明、构造函数都无法初始化，百歌谷度了一通后没有任何答案，无奈只能在构造函数中声明一个局部变量数组并初始化，然后用memcpy，（成员变量名为Sbox/InvSbox，局部变量名sBox/invsBox）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::SubBytes(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">5</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = Sbox[state[r][c]];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2>ShiftRows（行移位变换）</h2>

<p>行移位变换完成基于行的循环位移操作，变换方法：<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103102010146.png" alt="enter description here"><br>即行移位变换作用于行上，第0行不变，第1行循环左移1个字节，第2行循环左移2个字节，第3行循环左移3个字节。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::ShiftRows(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">7</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> t[<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">1</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            t[c] = state[r][(c+r)%<span class="number">4</span>];  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = t[c];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2>MixColumns（列混淆变换）</h2> 

<p> 逐列混合，方法：</p>
<p>b(x) = (03·x^3^ + 01·x^2^ + 01·x + 02) · a(x) mod(x^4^ + 1)<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103102014940.png" alt="enter description here"><br>矩阵表示形式：<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103102021588.png" alt="enter description here"></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::MixColumns(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">10</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> t[<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt; <span class="number">4</span>; c++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">        &#123;  </div><div class="line">            t[r] = state[r][c];  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = FFmul(<span class="number">0x02</span>, t[r])  </div><div class="line">                        ^ FFmul(<span class="number">0x03</span>, t[(r+<span class="number">1</span>)%<span class="number">4</span>])  </div><div class="line">                        ^ FFmul(<span class="number">0x01</span>, t[(r+<span class="number">2</span>)%<span class="number">4</span>])  </div><div class="line">                        ^ FFmul(<span class="number">0x01</span>, t[(r+<span class="number">3</span>)%<span class="number">4</span>]);  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">   </div><div class="line"><span class="keyword">unsigned</span> <span class="keyword">char</span> AES::FFmul(<span class="keyword">unsigned</span> <span class="keyword">char</span> a, <span class="keyword">unsigned</span> <span class="keyword">char</span> b)  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> bw[<span class="number">4</span>];  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> res=<span class="number">0</span>;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    bw[<span class="number">0</span>] = b;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;<span class="number">4</span>; i++)  </div><div class="line">    &#123;  </div><div class="line">        bw[i] = bw[i<span class="number">-1</span>]&lt;&lt;<span class="number">1</span>;  </div><div class="line">        <span class="keyword">if</span>(bw[i<span class="number">-1</span>]&amp;<span class="number">0x80</span>)  </div><div class="line">        &#123;  </div><div class="line">            bw[i]^=<span class="number">0x1b</span>;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;<span class="number">4</span>; i++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">if</span>((a&gt;&gt;i)&amp;<span class="number">0x01</span>)  </div><div class="line">        &#123;  </div><div class="line">            res ^= bw[i];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> res;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>其中FFmul为有限域GF(2^8^)上的乘法，标准算法应该是循环8次（b与a的每一位相乘，结果相加），但这里只用到最低2位，解密时用到的逆列混淆也只用了低4位，所以在这里高4位的运算是多余的，只计算低4位。</p>
<h3>AddRoundKey（轮密钥加变换）</h3> 

<p> 简单来说就是逐字节相加，有限域GF(2^8^)上的加法是模2加法，即异或</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::AddRoundKey(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">11</span>], <span class="keyword">unsigned</span> <span class="keyword">char</span> k[][<span class="number">12</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] ^= k[r][c];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3>KeyExpansion（密钥扩展）</h3>

<p> 将输入的密钥扩展为11组128位密钥组，其中第0组为输入密钥本身 </p>
<p> 其后第n组第i列 为 第n-1组第i列 与 第n组第i-1列之和（模2加法，1&lt;= i &lt;=3）<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103102042294.png" alt="enter description here"><br>对于每一组 第一列即i=0，有特殊的处理<br><img src="http://pic002.cnblogs.com/images/2010/169108/2010103102044067.png" alt="enter description here"><br>将前一列即第n-1组第3列的4个字节循环左移1个字节，</p>
<p>并对每个字节进行字节替代变换SubBytes</p>
<p>将第一行（即第一个字节）与轮常量rc[n]相加 </p>
<p>最后再与前一组该列相加 </p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::KeyExpansion(<span class="keyword">unsigned</span> <span class="keyword">char</span>* key, <span class="keyword">unsigned</span> <span class="keyword">char</span> w[][<span class="number">4</span>][<span class="number">4</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">int</span> i,j,r,c;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> rc[] = &#123;<span class="number">0x01</span>, <span class="number">0x02</span>, <span class="number">0x04</span>, <span class="number">0x08</span>, <span class="number">0x10</span>, <span class="number">0x20</span>, <span class="number">0x40</span>, <span class="number">0x80</span>, <span class="number">0x1b</span>, <span class="number">0x36</span>&#125;;  </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            w[<span class="number">0</span>][r][c] = key[r+c*<span class="number">4</span>];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;=<span class="number">10</span>; i++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>; j&lt;<span class="number">4</span>; j++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">unsigned</span> <span class="keyword">char</span> t[<span class="number">4</span>];  </div><div class="line">            <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">            &#123;  </div><div class="line">                t[r] = j ? w[i][r][j<span class="number">-1</span>] : w[i<span class="number">-1</span>][r][<span class="number">3</span>];  </div><div class="line">            &#125;  </div><div class="line">            <span class="keyword">if</span>(j == <span class="number">0</span>)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">unsigned</span> <span class="keyword">char</span> temp = t[<span class="number">0</span>];  </div><div class="line">                <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">3</span>; r++)  </div><div class="line">                &#123;  </div><div class="line">                    t[r] = Sbox[t[(r+<span class="number">1</span>)%<span class="number">4</span>]];  </div><div class="line">                &#125;  </div><div class="line">                t[<span class="number">3</span>] = Sbox[temp];  </div><div class="line">                t[<span class="number">0</span>] ^= rc[i<span class="number">-1</span>];  </div><div class="line">            &#125;  </div><div class="line">            <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">            &#123;  </div><div class="line">                w[i][r][j] = w[i<span class="number">-1</span>][r][j] ^ t[r];  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3>解密的基本运算</h3>

<p>AES解密算法与加密不同，基本运算中除了AddRoundKey（轮密钥加）不变外，其余的都需要进行逆变换，即</p>
<p>InvSubBytes（逆字节替代）、InvShiftRows（逆行移位）、InvMixColumns（逆列混淆）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span> AES::InvSubBytes(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">4</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = InvSbox[state[r][c]];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">   </div><div class="line"><span class="keyword">void</span> AES::InvShiftRows(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">4</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> t[<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">1</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            t[c] = state[r][(c-r+<span class="number">4</span>)%<span class="number">4</span>];  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span>; c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = t[c];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">   </div><div class="line"><span class="keyword">void</span> AES::InvMixColumns(<span class="keyword">unsigned</span> <span class="keyword">char</span> state[][<span class="number">4</span>])  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> t[<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> r,c;  </div><div class="line">    <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt; <span class="number">4</span>; c++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">        &#123;  </div><div class="line">            t[r] = state[r][c];  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = FFmul(<span class="number">0x0e</span>, t[r])  </div><div class="line">                        ^ FFmul(<span class="number">0x0b</span>, t[(r+<span class="number">1</span>)%<span class="number">4</span>])  </div><div class="line">                        ^ FFmul(<span class="number">0x0d</span>, t[(r+<span class="number">2</span>)%<span class="number">4</span>])  </div><div class="line">                        ^ FFmul(<span class="number">0x09</span>, t[(r+<span class="number">3</span>)%<span class="number">4</span>]);  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>加密过程</p>
<p>先将输入的明文按列序组合成4*4的矩阵，直接与第0组密钥（即输入的密钥）相加（异或），作为轮加密的输入</p>
<p>然后循环10次进行SubBytes、ShiftRows、MixColumns、AddRoundKey运算，最后恢复原序列</p>
<p>需要注意的是最后一轮并不进行MixColumns（列混淆变换）</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">unsigned</span> <span class="keyword">char</span>* AES::Cipher(<span class="keyword">unsigned</span> <span class="keyword">char</span>* input)  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> state[<span class="number">4</span>][<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> i,r,c;  </div><div class="line">   </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span> ;c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = input[c*<span class="number">4</span>+r];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">   </div><div class="line">    AddRoundKey(state,w[<span class="number">0</span>]);  </div><div class="line">   </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;=<span class="number">10</span>; i++)  </div><div class="line">    &#123;  </div><div class="line">        SubBytes(state);  </div><div class="line">        ShiftRows(state);  </div><div class="line">        <span class="keyword">if</span>(i!=<span class="number">10</span>)MixColumns(state);  </div><div class="line">        AddRoundKey(state,w[i]);  </div><div class="line">    &#125;  </div><div class="line">   </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span> ;c++)  </div><div class="line">        &#123;  </div><div class="line">            input[c*<span class="number">4</span>+r] = state[r][c];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">   </div><div class="line">    <span class="keyword">return</span> input;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>解密过程</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">unsigned</span> <span class="keyword">char</span>* AES::InvCipher(<span class="keyword">unsigned</span> <span class="keyword">char</span>* input)  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span> state[<span class="number">4</span>][<span class="number">4</span>];  </div><div class="line">    <span class="keyword">int</span> i,r,c;  </div><div class="line">   </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span> ;c++)  </div><div class="line">        &#123;  </div><div class="line">            state[r][c] = input[c*<span class="number">4</span>+r];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">   </div><div class="line">    AddRoundKey(state, w[<span class="number">10</span>]);  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">9</span>; i&gt;=<span class="number">0</span>; i--)  </div><div class="line">    &#123;  </div><div class="line">        InvShiftRows(state);  </div><div class="line">        InvSubBytes(state);  </div><div class="line">        AddRoundKey(state, w[i]);  </div><div class="line">        <span class="keyword">if</span>(i)InvMixColumns(state);  </div><div class="line">    &#125;  </div><div class="line">       </div><div class="line">    <span class="keyword">for</span>(r=<span class="number">0</span>; r&lt;<span class="number">4</span>; r++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">for</span>(c=<span class="number">0</span>; c&lt;<span class="number">4</span> ;c++)  </div><div class="line">        &#123;  </div><div class="line">            input[c*<span class="number">4</span>+r] = state[r][c];  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">   </div><div class="line">    <span class="keyword">return</span> input;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>对外部数据的加密/解密</p>
<p>至此已经实现了AES加密与解密的原型，在使用的时候一般处理的是字符串等，而不是直接传入128位的数据，所以要封装一下对外部数据的加解密处理</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">void</span>* AES::Cipher(<span class="keyword">void</span>* input, <span class="keyword">int</span> length)  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span>* in = (<span class="keyword">unsigned</span> <span class="keyword">char</span>*) input;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">if</span>(!length)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">while</span>(*(in+length++));  </div><div class="line">        in = (<span class="keyword">unsigned</span> <span class="keyword">char</span>*) input;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;length; i+=<span class="number">16</span>)  </div><div class="line">    &#123;  </div><div class="line">        Cipher(in+i);  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> input;  </div><div class="line">&#125;  </div><div class="line">   </div><div class="line"><span class="keyword">void</span>* AES::InvCipher(<span class="keyword">void</span>* input, <span class="keyword">int</span> length)  </div><div class="line">&#123;  </div><div class="line">    <span class="keyword">unsigned</span> <span class="keyword">char</span>* in = (<span class="keyword">unsigned</span> <span class="keyword">char</span>*) input;  </div><div class="line">    <span class="keyword">int</span> i;  </div><div class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;length; i+=<span class="number">16</span>)  </div><div class="line">    &#123;  </div><div class="line">        InvCipher(in+i);  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> input;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>加密时默认参数length=0,为要加密的数据长度，如果使用默认值，则作为字符串处理，以’\0’为结尾计算长度</p>
<p>加密时传进的指针要预留够16整数倍字节的空间，因为加密操作直接修改原数据，不足128位可能造成内存溢出</p>
<h2>SHA算法</h2>

<p>1 SHA1算法简介</p>
<p>　　安全哈希算法（Secure Hash Algorithm）主要适用于数字签名标准（Digital Signature Standard DSS）里面定义的数字签名算法（Digital Signature Algorithm DSA）。对于长度小于2^64位的消息，SHA1会产生一个160位的消息摘要。当接收到消息的时候，这个消息摘要可以用来验证数据的完整性。在传输的过程中，数据很可能会发生变化，那么这时候就会产生不同的消息摘要。</p>
<p>　　SHA1有如下特性：不可以从消息摘要中复原信息；两个不同的消息不会产生同样的消息摘要。</p>
<p>　　2 术语和概念</p>
<p>　　2.1位(Bit)，字节（Byte）和字（Word）</p>
<p>　　SHA1始终把消息当成一个位（bit）字符串来处理。本文中，一个“字”（Word）是32位，而一个“字节”（Byte）是8位。比如，字符串“abc”可以被转换成一个位字符串：01100001 01100010 01100011。它也可以被表示成16进制字符串: 0x616263.</p>
<p>　　2.2 运算符和符号</p>
<p>　　下面的逻辑运算符都被运用于“字”（Word）</p>
<p>　　X^Y = X，Y逻辑与</p>
<p>　　X \/ Y = X，Y逻辑或</p>
<p>　　X XOR Y= X，Y逻辑异或</p>
<p>　　~X = X逻辑取反</p>
<p>　　X+Y定义如下：</p>
<p>　　字X 和Y 代表两个整数x 和y, 其中0 &lt;= x &lt; 2^32 且0 &lt;= y &lt; 2^32. 令整数z = (x + y) mod 2^32. 这时候0 &lt;= z &lt; 2^32. 将z转换成字Z, 那么就是Z = X + Y. www.2cto.com</p>
<p>　　循环左移位操作符Sn(X)。X是一个字，n是一个整数，0&lt;=n&lt;=32。Sn(X) = (X&lt;<n)or(x>&gt;32-n)</n)or(x></p>
<p>　　X&lt;<n定义如下：抛弃最左边的n位数字，将各个位依次向左移动n位，然后用0填补右边的n位（最后结果还是32位）。x>&gt;n是抛弃右边的n位，将各个位依次向右移动n位，然后在左边的n位填0。因此可以叫Sn(X)位循环移位运算</n定义如下：抛弃最左边的n位数字，将各个位依次向左移动n位，然后用0填补右边的n位（最后结果还是32位）。x></p>
<p>　　3 SHA1算法描述</p>
<p>　　在SHA1算法中，我们必须把原始消息（字符串，文件等）转换成位字符串。SHA1算法只接受位作为输入。假设我们对字符串“abc”产生消息摘要。首先，我们将它转换成位字符串如下：</p>
<p>　　01100001 01100010 01100011</p>
<p>　　―――――――――――――</p>
<p>　　‘a’=97 ‘b’=98 ‘c’=99</p>
<p>　　这个位字符串的长度为24。下面我们需要5个步骤来计算MD5。</p>
<p>　　3.1 补位</p>
<p>　　消息必须进行补位，以使其长度在对512取模以后的余数是448。也就是说，（补位后的消息长度）%512 = 448。即使长度已经满足对512取模后余数是448，补位也必须要进行。</p>
<p>　　补位是这样进行的：先补一个1，然后再补0，直到长度满足对512取模后余数是448。总而言之，补位是至少补一位，最多补512位。还是以前面的“abc”为例显示补位的过程。</p>
<p>　　原始信息：01100001 01100010 01100011</p>
<p>　　补位第一步：01100001 01100010 01100011 1</p>
<p>　　首先补一个“1”</p>
<p>　　补位第二步：01100001 01100010 01100011 10…..0</p>
<p>　　然后补423个“0”</p>
<p>　　我们可以把最后补位完成后的数据用16进制写成下面的样子</p>
<p>　　61626380 00000000 00000000 00000000</p>
<p>　　00000000 00000000 00000000 00000000</p>
<p>　　00000000 00000000 00000000 00000000</p>
<p>　　00000000 00000000</p>
<p>　　现在，数据的长度是448了，我们可以进行下一步操作。</p>
<p>　　3.2 补长度</p>
<p>　　所谓的补长度是将原始数据的长度补到已经进行了补位操作的消息后面。通常用一个64位的数据来表示原始消息的长度。如果消息长度不大于2^64，那么第一个字就是0。在进行了补长度的操作以后，整个消息就变成下面这样了（16进制格式）</p>
<p>　　61626380 00000000 00000000 00000000</p>
<p>　　00000000 00000000 00000000 00000000</p>
<p>　　00000000 00000000 00000000 00000000</p>
<p>　　00000000 00000000 00000000 00000018</p>
<p>　　如果原始的消息长度超过了512，我们需要将它补成512的倍数。然后我们把整个消息分成一个一个512位的数据块，分别处理每一个数据块，从而得到消息摘要。</p>
<p>　　3.3 使用的常量</p>
<p>　　一系列的常量字K(0), K(1), … , K(79)，如果以16进制给出。它们如下：</p>
<p>　　Kt = 0x5A827999 (0 &lt;= t &lt;= 19)</p>
<p>　　Kt = 0x6ED9EBA1 (20 &lt;= t &lt;= 39)</p>
<p>　　Kt = 0x8F1BBCDC (40 &lt;= t &lt;= 59)</p>
<p>　　Kt = 0xCA62C1D6 (60 &lt;= t &lt;= 79).</p>
<p>　　3.4 需要使用的函数</p>
<p>　　在SHA1中我们需要一系列的函数。每个函数ft (0 &lt;= t &lt;= 79)都操作32位字B，C，D并且产生32位字作为输出。ft(B,C,D)可以如下定义</p>
<p>　　ft(B,C,D) = (B AND C) or ((NOT B) AND D) ( 0 &lt;= t &lt;= 19)</p>
<p>　　ft(B,C,D) = B XOR C XOR D (20 &lt;= t &lt;= 39)</p>
<p>　　ft(B,C,D) = (B AND C) or (B AND D) or (C AND D) (40 &lt;= t &lt;= 59)</p>
<p>　　ft(B,C,D) = B XOR C XOR D (60 &lt;= t &lt;= 79).</p>
<p>　　3.5 计算消息摘要</p>
<p>　　必须使用进行了补位和补长度后的消息来计算消息摘要。计算需要两个缓冲区，每个都由5个32位的字组成，还需要一个80个32位字的缓冲区。第一个5个字的缓冲区被标识为A，B，C，D，E。第二个5个字的缓冲区被标识为H0, H1, H2, H3, H4</p>
<p>　　。80个字的缓冲区被标识为W0, W1,…, W79</p>
<p>　　另外还需要一个一个字的TEMP缓冲区。</p>
<p>　　为了产生消息摘要，在第4部分中定义的16个字的数据块M1, M2,…, Mn</p>
<p>　　会依次进行处理，处理每个数据块Mi 包含80个步骤。</p>
<p>　　在处理每个数据块之前，缓冲区{Hi} 被初始化为下面的值（16进制）</p>
<p>　　H0 = 0x67452301</p>
<p>　　H1 = 0xEFCDAB89</p>
<p>　　H2 = 0x98BADCFE</p>
<p>　　H3 = 0x10325476</p>
<p>　　H4 = 0xC3D2E1F0.</p>
<p>　　现在开始处理M1, M2, … , Mn。为了处理Mi,需要进行下面的步骤</p>
<p>　　(1).将Mi 分成16 个字W0, W1, … , W15, W0 是最左边的字</p>
<p>　　(2).对于t = 16 到79 令Wt = S1(Wt-3 XOR Wt-8 XOR Wt- 14 XOR Wt-16).</p>
<p>　　(3).令A = H0, B = H1, C = H2, D = H3, E = H4.</p>
<p>　　(4) 对于t = 0 到79，执行下面的循环</p>
<p>　　TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;</p>
<p>　　E = D; D = C; C = S30(B); B = A; A = TEMP;</p>
<p>　　(5).令H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.</p>
<p>　　在处理完所有的Mn, 后，消息摘要是一个160位的字符串，以下面的顺序标识</p>
<p>　　H0 H1 H2 H3 H4.</p>
<p>　　对于SHA256,SHA384,SHA512。你也可以用相似的办法来计算消息摘要。对消息进行补位的算法完全是一样的。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">class</span> SHA1Util &#123;  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> final boolean hexcase = <span class="literal">false</span>;  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> final String b64pad = <span class="string">"="</span>;  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> final <span class="keyword">int</span> chrsz = <span class="number">8</span>;  </div><div class="line">  </div><div class="line">    <span class="comment">// 得到字符串SHA-1值的方法  </span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">hex_sha1</span><span class="params">(String s)</span> </span>&#123;  </div><div class="line">        s = (s == null) ? <span class="string">""</span> : s;  </div><div class="line">        <span class="keyword">return</span> binb2hex(core_sha1(str2binb(s), s.length() * chrsz));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">b64_hmac_sha1</span><span class="params">(String key, String data)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> binb2b64(core_hmac_sha1(key, data));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">b64_sha1</span><span class="params">(String s)</span> </span>&#123;  </div><div class="line">        s = (s == null) ? <span class="string">""</span> : s;  </div><div class="line">        <span class="keyword">return</span> binb2b64(core_sha1(str2binb(s), s.length() * chrsz));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">binb2b64</span><span class="params">(<span class="keyword">int</span>[] binarray)</span> </span>&#123;  </div><div class="line">        String tab = <span class="string">"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"</span>;  </div><div class="line">        String str = <span class="string">""</span>;  </div><div class="line">        binarray = strechbinarray(binarray, binarray.length * <span class="number">4</span>);  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; binarray.length * <span class="number">4</span>; i += <span class="number">3</span>) &#123;  </div><div class="line">            <span class="keyword">int</span> triplet = (((binarray[i &gt;&gt; <span class="number">2</span>] &gt;&gt; <span class="number">8</span> * (<span class="number">3</span> - i % <span class="number">4</span>)) &amp; <span class="number">0xff</span>) &lt;&lt; <span class="number">16</span>)  </div><div class="line">                    | (((binarray[i + <span class="number">1</span> &gt;&gt; <span class="number">2</span>] &gt;&gt; <span class="number">8</span> * (<span class="number">3</span> - (i + <span class="number">1</span>) % <span class="number">4</span>)) &amp; <span class="number">0xff</span>) &lt;&lt; <span class="number">8</span>)  </div><div class="line">                    | ((binarray[i + <span class="number">2</span> &gt;&gt; <span class="number">2</span>] &gt;&gt; <span class="number">8</span> * (<span class="number">3</span> - (i + <span class="number">2</span>) % <span class="number">4</span>)) &amp; <span class="number">0xff</span>);  </div><div class="line">  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">4</span>; j++) &#123;  </div><div class="line">                <span class="keyword">if</span> (i * <span class="number">8</span> + j * <span class="number">6</span> &gt; binarray.length * <span class="number">32</span>) &#123;  </div><div class="line">                    str += b64pad;  </div><div class="line">                &#125; <span class="keyword">else</span> &#123;  </div><div class="line">                    str += tab.charAt((triplet &gt;&gt; <span class="number">6</span> * (<span class="number">3</span> - j)) &amp; <span class="number">0x3f</span>);  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> cleanb64str(str);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">binb2hex</span><span class="params">(<span class="keyword">int</span>[] binarray)</span> </span>&#123;  </div><div class="line">        String hex_tab = hexcase ? <span class="string">"0123456789abcdef"</span> : <span class="string">"0123456789abcdef"</span>;  </div><div class="line">        String str = <span class="string">""</span>;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; binarray.length * <span class="number">4</span>; i++) &#123;  </div><div class="line">            <span class="keyword">char</span> a = (<span class="keyword">char</span>) hex_tab.charAt((binarray[i &gt;&gt; <span class="number">2</span>] &gt;&gt; ((<span class="number">3</span> - i % <span class="number">4</span>) * <span class="number">8</span> + <span class="number">4</span>)) &amp; <span class="number">0xf</span>);  </div><div class="line">            <span class="keyword">char</span> b = (<span class="keyword">char</span>) hex_tab.charAt((binarray[i &gt;&gt; <span class="number">2</span>] &gt;&gt; ((<span class="number">3</span> - i % <span class="number">4</span>) * <span class="number">8</span>)) &amp; <span class="number">0xf</span>);  </div><div class="line">            str += (<span class="keyword">new</span> Character(a).toString() + <span class="keyword">new</span> Character(b).toString());  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> str;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">binb2str</span><span class="params">(<span class="keyword">int</span>[] bin)</span> </span>&#123;  </div><div class="line">        String str = <span class="string">""</span>;  </div><div class="line">        <span class="keyword">int</span> mask = (<span class="number">1</span> &lt;&lt; chrsz) - <span class="number">1</span>;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; bin.length * <span class="number">32</span>; i += chrsz) &#123;  </div><div class="line">            str += (<span class="keyword">char</span>) ((bin[i &gt;&gt; <span class="number">5</span>] &gt;&gt;&gt; (<span class="number">24</span> - i % <span class="number">32</span>)) &amp; mask);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> str;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">bit_rol</span><span class="params">(<span class="keyword">int</span> num, <span class="keyword">int</span> cnt)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> (num &lt;&lt; cnt) | (num &gt;&gt;&gt; (<span class="number">32</span> - cnt));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> String <span class="title">cleanb64str</span><span class="params">(String str)</span> </span>&#123;  </div><div class="line">        str = (str == null) ? <span class="string">""</span> : str;  </div><div class="line">        <span class="keyword">int</span> len = str.length();  </div><div class="line">  </div><div class="line">        <span class="keyword">if</span> (len &lt;= <span class="number">1</span>) &#123;  </div><div class="line">            <span class="keyword">return</span> str;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">char</span> trailchar = str.charAt(len - <span class="number">1</span>);  </div><div class="line">        String trailstr = <span class="string">""</span>;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = len - <span class="number">1</span>; i &gt;= <span class="number">0</span> &amp;&amp; str.charAt(i) == trailchar; i--) &#123;  </div><div class="line">            trailstr += str.charAt(i);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> str.substring(<span class="number">0</span>, str.indexOf(trailstr));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] complete216(<span class="keyword">int</span>[] oldbin) &#123;  </div><div class="line">        <span class="keyword">if</span> (oldbin.length &gt;= <span class="number">16</span>) &#123;  </div><div class="line">            <span class="keyword">return</span> oldbin;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] newbin = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">16</span> - oldbin.length];  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; newbin.length; newbin[i] = <span class="number">0</span>, i++)  </div><div class="line">            ;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> concat(oldbin, newbin);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] concat(<span class="keyword">int</span>[] oldbin, <span class="keyword">int</span>[] newbin) &#123;  </div><div class="line">        <span class="keyword">int</span>[] retval = <span class="keyword">new</span> <span class="keyword">int</span>[oldbin.length + newbin.length];  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; (oldbin.length + newbin.length); i++) &#123;  </div><div class="line">            <span class="keyword">if</span> (i &lt; oldbin.length) &#123;  </div><div class="line">                retval[i] = oldbin[i];  </div><div class="line">            &#125; <span class="keyword">else</span> &#123;  </div><div class="line">                retval[i] = newbin[i - oldbin.length];  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> retval;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] core_hmac_sha1(String key, String data) &#123;  </div><div class="line">        key = (key == null) ? <span class="string">""</span> : key;  </div><div class="line">        data = (data == null) ? <span class="string">""</span> : data;  </div><div class="line">        <span class="keyword">int</span>[] bkey = complete216(str2binb(key));  </div><div class="line">  </div><div class="line">        <span class="keyword">if</span> (bkey.length &gt; <span class="number">16</span>) &#123;  </div><div class="line">            bkey = core_sha1(bkey, key.length() * chrsz);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] ipad = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">16</span>];  </div><div class="line">        <span class="keyword">int</span>[] opad = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">16</span>];  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">16</span>; ipad[i] = <span class="number">0</span>, opad[i] = <span class="number">0</span>, i++)  </div><div class="line">            ;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">16</span>; i++) &#123;  </div><div class="line">            ipad[i] = bkey[i] ^ <span class="number">0x36363636</span>;  </div><div class="line">            opad[i] = bkey[i] ^ <span class="number">0x5c5c5c5c</span>;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] hash = core_sha1(concat(ipad, str2binb(data)), <span class="number">512</span> + data.length() * chrsz);  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> core_sha1(concat(opad, hash), <span class="number">512</span> + <span class="number">160</span>);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] core_sha1(<span class="keyword">int</span>[] x, <span class="keyword">int</span> len) &#123;  </div><div class="line">        <span class="keyword">int</span> size = (len &gt;&gt; <span class="number">5</span>);  </div><div class="line">        x = strechbinarray(x, size);  </div><div class="line">        x[len &gt;&gt; <span class="number">5</span>] |= <span class="number">0x80</span> &lt;&lt; (<span class="number">24</span> - len % <span class="number">32</span>);  </div><div class="line">        size = ((len + <span class="number">64</span> &gt;&gt; <span class="number">9</span>) &lt;&lt; <span class="number">4</span>) + <span class="number">15</span>;  </div><div class="line">        x = strechbinarray(x, size);  </div><div class="line">        x[((len + <span class="number">64</span> &gt;&gt; <span class="number">9</span>) &lt;&lt; <span class="number">4</span>) + <span class="number">15</span>] = len;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] w = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">80</span>];  </div><div class="line">        <span class="keyword">int</span> a = <span class="number">1732584193</span>;  </div><div class="line">        <span class="keyword">int</span> b = <span class="number">-271733879</span>;  </div><div class="line">        <span class="keyword">int</span> c = <span class="number">-1732584194</span>;  </div><div class="line">        <span class="keyword">int</span> d = <span class="number">271733878</span>;  </div><div class="line">        <span class="keyword">int</span> e = <span class="number">-1009589776</span>;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; x.length; i += <span class="number">16</span>) &#123;  </div><div class="line">            <span class="keyword">int</span> olda = a;  </div><div class="line">            <span class="keyword">int</span> oldb = b;  </div><div class="line">            <span class="keyword">int</span> oldc = c;  </div><div class="line">            <span class="keyword">int</span> oldd = d;  </div><div class="line">            <span class="keyword">int</span> olde = e;  </div><div class="line">  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">80</span>; j++) &#123;  </div><div class="line">                <span class="keyword">if</span> (j &lt; <span class="number">16</span>) &#123;  </div><div class="line">                    w[j] = x[i + j];  </div><div class="line">                &#125; <span class="keyword">else</span> &#123;  </div><div class="line">                    w[j] = rol(w[j - <span class="number">3</span>] ^ w[j - <span class="number">8</span>] ^ w[j - <span class="number">14</span>] ^ w[j - <span class="number">16</span>], <span class="number">1</span>);  </div><div class="line">                &#125;  </div><div class="line">  </div><div class="line">                <span class="keyword">int</span> t = safe_add(safe_add(rol(a, <span class="number">5</span>), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));  </div><div class="line">  </div><div class="line">                e = d;  </div><div class="line">                d = c;  </div><div class="line">                c = rol(b, <span class="number">30</span>);  </div><div class="line">                b = a;  </div><div class="line">                a = t;  </div><div class="line">            &#125;  </div><div class="line">  </div><div class="line">            a = safe_add(a, olda);  </div><div class="line">            b = safe_add(b, oldb);  </div><div class="line">            c = safe_add(c, oldc);  </div><div class="line">            d = safe_add(d, oldd);  </div><div class="line">            e = safe_add(e, olde);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] retval = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">5</span>];  </div><div class="line">  </div><div class="line">        retval[<span class="number">0</span>] = a;  </div><div class="line">        retval[<span class="number">1</span>] = b;  </div><div class="line">        retval[<span class="number">2</span>] = c;  </div><div class="line">        retval[<span class="number">3</span>] = d;  </div><div class="line">        retval[<span class="number">4</span>] = e;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> retval;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">dotest</span><span class="params">()</span> </span>&#123;  </div><div class="line">        String key = <span class="string">"key"</span>;  </div><div class="line">        String data = <span class="string">"data"</span>;  </div><div class="line">        System.out.println(<span class="string">"hex_sha1("</span> + data + <span class="string">")="</span> + hex_sha1(data));  </div><div class="line">        System.out.println(<span class="string">"b64_sha1("</span> + data + <span class="string">")="</span> + b64_sha1(data));  </div><div class="line">        System.out.println(<span class="string">"str_sha1("</span> + data + <span class="string">")="</span> + str_sha1(data));  </div><div class="line">        System.out.println(<span class="string">"hex_hmac_sha1("</span> + key + <span class="string">","</span> + data + <span class="string">")="</span> + hex_hmac_sha1(key, data));  </div><div class="line">        System.out.println(<span class="string">"b64_hmac_sha1("</span> + key + <span class="string">","</span> + data + <span class="string">")="</span> + b64_hmac_sha1(key, data));  </div><div class="line">        System.out.println(<span class="string">"str_hmac_sha1("</span> + key + <span class="string">","</span> + data + <span class="string">")="</span> + str_hmac_sha1(key, data));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">hex_hmac_sha1</span><span class="params">(String key, String data)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> binb2hex(core_hmac_sha1(key, data));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">rol</span><span class="params">(<span class="keyword">int</span> num, <span class="keyword">int</span> cnt)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> (num &lt;&lt; cnt) | (num &gt;&gt;&gt; (<span class="number">32</span> - cnt));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">safe_add</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span> </span>&#123;  </div><div class="line">        <span class="keyword">int</span> lsw = (<span class="keyword">int</span>) (x &amp; <span class="number">0xffff</span>) + (<span class="keyword">int</span>) (y &amp; <span class="number">0xffff</span>);  </div><div class="line">        <span class="keyword">int</span> msw = (x &gt;&gt; <span class="number">16</span>) + (y &gt;&gt; <span class="number">16</span>) + (lsw &gt;&gt; <span class="number">16</span>);  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> (msw &lt;&lt; <span class="number">16</span>) | (lsw &amp; <span class="number">0xffff</span>);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">sha1_ft</span><span class="params">(<span class="keyword">int</span> t, <span class="keyword">int</span> b, <span class="keyword">int</span> c, <span class="keyword">int</span> d)</span> </span>&#123;  </div><div class="line">        <span class="keyword">if</span> (t &lt; <span class="number">20</span>)  </div><div class="line">            <span class="keyword">return</span> (b &amp; c) | ((~b) &amp; d);  </div><div class="line">  </div><div class="line">        <span class="keyword">if</span> (t &lt; <span class="number">40</span>)  </div><div class="line">            <span class="keyword">return</span> b ^ c ^ d;  </div><div class="line">  </div><div class="line">        <span class="keyword">if</span> (t &lt; <span class="number">60</span>)  </div><div class="line">            <span class="keyword">return</span> (b &amp; c) | (b &amp; d) | (c &amp; d);  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> b ^ c ^ d;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">sha1_kt</span><span class="params">(<span class="keyword">int</span> t)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> (t &lt; <span class="number">20</span>) ? <span class="number">1518500249</span> : (t &lt; <span class="number">40</span>) ? <span class="number">1859775393</span> : (t &lt; <span class="number">60</span>) ? <span class="number">-1894007588</span> : <span class="number">-899497514</span>;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> boolean <span class="title">sha1_vm_test</span><span class="params">()</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> hexcase ? hex_sha1(<span class="string">"abc"</span>).equals(<span class="string">"a9993e364706816aba3e25717850c26c9cd0d89d"</span>) : hex_sha1(<span class="string">"abc"</span>).equals(  </div><div class="line">                <span class="string">"a9993e364706816aba3e25717850c26c9cd0d89d"</span>);  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">str_hmac_sha1</span><span class="params">(String key, String data)</span> </span>&#123;  </div><div class="line">        <span class="keyword">return</span> binb2str(core_hmac_sha1(key, data));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">str_sha1</span><span class="params">(String s)</span> </span>&#123;  </div><div class="line">        s = (s == null) ? <span class="string">""</span> : s;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> binb2str(core_sha1(str2binb(s), s.length() * chrsz));  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] str2binb(String str) &#123;  </div><div class="line">        str = (str == null) ? <span class="string">""</span> : str;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] tmp = <span class="keyword">new</span> <span class="keyword">int</span>[str.length() * chrsz];  </div><div class="line">        <span class="keyword">int</span> mask = (<span class="number">1</span> &lt;&lt; chrsz) - <span class="number">1</span>;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; str.length() * chrsz; i += chrsz) &#123;  </div><div class="line">            tmp[i &gt;&gt; <span class="number">5</span>] |= ((<span class="keyword">int</span>) (str.charAt(i / chrsz)) &amp; mask) &lt;&lt; (<span class="number">24</span> - i % <span class="number">32</span>);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span> len = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tmp.length &amp;&amp; tmp[i] != <span class="number">0</span>; i++, len++)  </div><div class="line">            ;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] bin = <span class="keyword">new</span> <span class="keyword">int</span>[len];  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; len; i++) &#123;  </div><div class="line">            bin[i] = tmp[i];  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> bin;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span>[] strechbinarray(<span class="keyword">int</span>[] oldbin, <span class="keyword">int</span> size) &#123;  </div><div class="line">        <span class="keyword">int</span> currlen = oldbin.length;  </div><div class="line">  </div><div class="line">        <span class="keyword">if</span> (currlen &gt;= size + <span class="number">1</span>) &#123;  </div><div class="line">            <span class="keyword">return</span> oldbin;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">int</span>[] newbin = <span class="keyword">new</span> <span class="keyword">int</span>[size + <span class="number">1</span>];  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; size; newbin[i] = <span class="number">0</span>, i++)  </div><div class="line">            ;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; currlen; i++) &#123;  </div><div class="line">            newbin[i] = oldbin[i];  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">return</span> newbin;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span> </span>&#123;  </div><div class="line">        System.out.println(<span class="string">"admin的SHA1的值为："</span> + hex_sha1(<span class="string">"admin"</span>) + <span class="string">",length="</span> + hex_sha1(<span class="string">"admin"</span>).length());  </div><div class="line">    &#125;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>原文：<a href="http://blog.csdn.net/z1x1c1v1/article/details/8755032" target="_blank" rel="external">http://blog.csdn.net/z1x1c1v1/article/details/8755032</a></p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2015/10/16/algorithm-detail-DES-AES-SHA/">DES、AES、SHA加密算法详解</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">Dragonflyxyz</a></p>
        <p><span>发布时间:</span>2015-10-16, 18:54:23</p>
        <p><span>最后更新:</span>2016-10-16, 19:17:03</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2015/10/16/algorithm-detail-DES-AES-SHA/" title="DES、AES、SHA加密算法详解">https://dragonflyxyz.github.io/2015/10/16/algorithm-detail-DES-AES-SHA/</a>
            <span class="copy-path" data-clipboard-text="原文: https://dragonflyxyz.github.io/2015/10/16/algorithm-detail-DES-AES-SHA/　　作者: Dragonflyxyz" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2015/11/29/photo-wall-style/">
                    教你轻松搞定相片墙壁搭配
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2015/10/16/homedesign-north-america/">
                    越来越喜欢北美的家庭装饰风格
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#undefined"><span class="toc-number">1.</span> <span class="toc-text">AES算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#undefined"><span class="toc-number">1.1.</span> <span class="toc-text">ByteSubstitution（字节替代）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#undefined"><span class="toc-number">2.</span> <span class="toc-text">ShiftRows（行移位变换）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#undefined"><span class="toc-number">3.</span> <span class="toc-text">MixColumns（列混淆变换）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#undefined"><span class="toc-number">3.1.</span> <span class="toc-text">AddRoundKey（轮密钥加变换）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#undefined"><span class="toc-number">3.2.</span> <span class="toc-text">KeyExpansion（密钥扩展）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#undefined"><span class="toc-number">3.3.</span> <span class="toc-text">解密的基本运算</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#undefined"><span class="toc-number">4.</span> <span class="toc-text">SHA算法</span></a></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"DES、AES、SHA加密算法详解　| Dragonflyxyz的知识整理　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2015/11/29/photo-wall-style/" title="上一篇: 教你轻松搞定相片墙壁搭配">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2015/10/16/homedesign-north-america/" title="下一篇: 越来越喜欢北美的家庭装饰风格">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2018/02/03/book-c-sharp-deconstructed/">Book《C# Deconstructed-Discover How C# Works on the .Net Framework》</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/26/mathematics-rookie-and-machine-learning/">数学菜鸟如何学习机器学习</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/23/automatic-sketchup-cn-3/">《Automatic SketchUp Creating 3-D Models in Ruby》中文翻译 11-13章</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/10/automatic-sketchup-cn-2/">《Automatic SketchUp Creating 3-D Models in Ruby》中文翻译7-10章</a></li><li class="post-list-item"><a class="post-list-link" href="/2017/03/09/automatic-sketchup-cn-1/">《Automatic SketchUp Creating 3-D Models in Ruby》中文翻译1-5章</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/12/29/python-opencv-learn-note01/">Python OpenCV学习记录</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/11/15/learn-deep-learning-zero-based-1/">零基础入门深度学习(1)-感知器</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/11/15/hexo-show-mathjax-and-table/">Hexo如何显示公式和表格</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/23/hexo-local-search-only-titile/">让Hexo的本地搜索只搜索标题</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/17/why-im-switching-from-python-to-nodejs/">为什么我从Python转战到Node.js</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/16/php-develop-1200examples/">《PHP开发实战1200例》</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/16/introduction-john-carmack/">第一人称射击游戏教父，首款3D游戏的开发者，传奇程序员——约翰·卡马克</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/16/Ebook-search-engine/">几个好的电子书搜索引擎</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/16/MIT-tech-review-2016/">麻省理工科技评论2016年1-6月</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/10/16/head-first-python/">《Head First Python》中文版</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/07/09/microsoft-opensource-malmo-ai/">微软开源Malmo AI项目平台 利用Minecraft构建复杂机器人任务</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/06/22/why-this-robot-in-the-grocery-store/">零售业有福了！大数据机器人卖场值班，点货超轻松</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/06/21/poor-zorro-linux-book/">穷佐罗的Linux书，知识点整理的很好</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/20/github-awesome-hadoop/">Hadoop学习资源集合（Github）</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/17/python-decorated-concurrency/">Python多线程(Decorated concurrency)</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/15/github-awesome-machine-learning/">Github上最全的机器学习资料集合</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/12/the-9-best-languages-form-crunching-data/">数据处理的 9 大编程语言</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/05/github-awesome-bigdata/">最全的大数据学习资料集合（Github）</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/05/01/a-low-scientist-how-to-get-nobel-prize/">一个逗逼的科研狗是如何成为诺贝尔奖得主的</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/04/23/how-much-a-chip/">一枚芯片的实际成本是多少？</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/04/22/awesome-python-site/">Awesome-python一个收集Python资源的网站</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/04/21/intel-releases-the-arduino-101-firmware-source-code/">Intel将两款arduino设备的实时操作系统开源</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/04/20/studying-the-language-and-structure-in-non-programmers-solutions/">《编程问题的非程序员解决方案之研究》</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/26/if-human-and-animals-change/">如果人和动物角色互换？这样的世界怎样想象</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/11/checksum-8bits-and-16bits/">checksum-8位和16位校验和代码示例</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/02/06/easy-popular-science-doc-deep-learning/">关于深度学习可能最容易读进去的科普贴</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/12/16/what-is-iot-ppt/">物联网是什么-ppt图</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/12/14/100-BBC-recordfile-parents-with-children/">家长应该陪孩子一起看的100部BBC纪录片</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/11/30/america-people-live-style/">美国日常生活习惯一瞥</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/11/29/photo-wall-style/">教你轻松搞定相片墙壁搭配</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/10/16/algorithm-detail-DES-AES-SHA/">DES、AES、SHA加密算法详解</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/10/16/homedesign-north-america/">越来越喜欢北美的家庭装饰风格</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/03/06/mysql-10millions-optimize-query/">Mysql数据库千万级别数据的查询优化和分页测试</a></li><li class="post-list-item"><a class="post-list-link" href="/2015/02/16/my-blog-set/">博客设置</a></li><li class="post-list-item"><a class="post-list-link" href="/2014/10/01/john-carmack-on-inlined-code/">John Carmack on inlined code</a></li><li class="post-list-item"><a class="post-list-link" href="/2014/05/01/hello-world/">Hello World</a></li><li class="post-list-item"><a class="post-list-link" href="/2014/01/16/test1/">test1</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2018 Dragonflyxyz
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div><!-- hexo-inject:begin --><!-- Begin: Injected MathJax -->
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({"tex2jax":{"inlineMath":[["$","$"],["\\(","\\)"]],"skipTags":["script","noscript","style","textarea","pre","code"],"processEscapes":true},"TeX":{"equationNumbers":{"autoNumber":"AMS"}}});
</script>

<script type="text/x-mathjax-config">
  MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
      all[i].SourceElement().parentNode.className += ' has-jax';
    }
  });
</script>

<script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- End: Injected MathJax -->
<!-- hexo-inject:end -->
</body>
</html>